#
#
#   NRK plugin for XBMC Media center
#
# Copyright (C) 2009 Victor Vikene  contact: z0py3r@hotmail.com
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
#
#

import sys

__settings__ = sys.modules[ "__main__" ].__settings__
__language__ = sys.modules[ "__main__" ].__language__


import os
import api_nrk as nrk
import xbmc
from utils import Key, Plugin, Log, State, PluginError
from xbmcgui import ListItem
from xbmcplugin import addDirectoryItem, endOfDirectory, getSetting

Log.debug( "addon.NRK.load ::  '%s'" % __name__ )

plugin = Plugin()
lang = plugin.language

class Main:
    
    def add(self, label, prefix, type, id=None, img='', icon='', isdir=True, commands=None):
    
        if img != '':
            img = plugin.image(img)
            
        url = Key.build_url(prefix, type=type, id=id)
        li  = ListItem(label, iconImage=icon, thumbnailImage=img)
        if commands:
            li.addContextMenuItems( commands, True )
        ok  = addDirectoryItem(self.hndl, url=url, listitem=li, isFolder=isdir)
        
        return ok
        
        
        
    def __init__(self):
    
        self.hndl = int(sys.argv[1])
     
     
        self.add( lang(30250),  nrk.PROGRAM,  nrk.PROGRAM,   img='ico_program.png')
        self.add( lang(30252),  nrk.CHANNELS, nrk.CHANNELS,  img='ico_tv.png')
        self.add( lang(30258),  'webradio',  'webradio',     img='ico_radio.png')
        self.add( lang(30264),  'other',     'podcastgroup', img='ico_podcast.png')
        self.add( lang(30263),  'other',     'other',        img='ico_other.png')

        """
        ----------------------------------------------------------------------      
        Teletext not available atm
        
        From nrk.no: 
           Vi regner med at en ny og bedre loesning vil vaere 
           paa plass i loepet av sommeren 2010.
        ----------------------------------------------------------------------
        
        commands = []
        commands.append(( lang(30800), 
                        'XBMC.RunPlugin(%s)' % ( Key.build_url('teletext', page=101)), 
                        ))
        commands.append(( lang(30801), 
                        'XBMC.RunPlugin(%s)' % ( Key.build_url('teletext', page=131)), 
                        ))
        commands.append(( lang(30802), 
                        'XBMC.RunPlugin(%s)' % ( Key.build_url('teletext', page=200)), 
                        ))
        commands.append(( lang(30803), 
                        'XBMC.RunPlugin(%s)' % ( Key.build_url('teletext', page=300)), 
                        ))
        commands.append(( lang(30804), 
                        'XBMC.RunPlugin(%s)' % ( Key.build_url('teletext', page=590)), 
                        ))
        self.add( lang(30261),      'teletext', 'teletext', img='ttv-icon.png', isdir=False, commands=commands)
        """
        
        # Only add the entry for Favourites if the xml file containing favourites exists already.
        if os.path.isfile(plugin.favorites):
          self.add(lang(30262), 'favorites', 'favorites', img='ico_favorites.png')
          
        endOfDirectory(self.hndl)